// pages/app/certification/index/index.js
const app = getApp();
var host = app.globalData.host;
var utils = app.globalData.utils;
var self = null;

Page({
  data: {
    themeColor: '',
    defaultColor: '',
    borderColor: '',
    list: [],
    pageNumber: 1,
    totalPage: 1
  },
  onLoad: function (options) {
    self = this;
    self.getStyle();
  },
  onShow: function () {
    self.init();
  },
  getStyle() {
    self.setData({
      themeColor: app.globalData.themeColor,
      defaultColor: app.globalData.defaultColor,
      borderColor: app.globalData.borderColor
    })
  },
  init() {
    var url = host + '/lx/mini/question/list';
    var params = {};
    var success = function (res) {
      self.setData({
        list: res.info.list,
        pageNumber: res.info.pageNumber,
        totalPage: res.info.totalPage
      });
    }
    var fail = function (res) {
      console.log(res);
    };
    var complete = function () { };
    utils.request(url, params, success, fail, complete);
  },
  onTapCourse(event) {
    wx.checkSession({
      success() {
        app.login(self.route);
      },
      fail() {
        app.loginWechat(app.login(self.route));
      }
    })
    var id = event.currentTarget.dataset.id;
    if (app.globalData.userInfo) {
      self.goCourse(id);
    } else {
      app.userInfoReadyCallback = res => {
        self.goCourse(id);
        app.userInfoReadyCallback = null;
      }
    }
  },
  goCourse(id) {
    var user = app.globalData.userInfo;
    if (user.status == 3) {
      wx.showModal({
        title: '您没有通过审核',
        content: '不能参加培训考试。' + '原因：' + user.content,
        showCancel: false
      })
      return;
    }
    wx.navigateTo({
      url: '/pages/app/certification/courses/courses?id=' + id
    })
  }
})